package com.xfcy.blog.controller.system;



import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xfcy.blog.annotation.OperationLogger;
import com.xfcy.blog.common.R;
import com.xfcy.blog.dto.SystemUserDTO;
import com.xfcy.blog.entity.User;
import com.xfcy.blog.service.UserService;
import com.xfcy.blog.valid.AddGroup;
import com.xfcy.blog.valid.UpdateGroup;
import com.xfcy.blog.vo.SystemUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统管理-用户基础信息表(User)表控制层
 *
 * @author 晓风残月Lx
 * @since 2023-04-09 11:03:06
 */
@RestController
@Api(tags = "系统用户管理-接口")
@RequestMapping("/system/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/getCurrentUserInfo")
    @SaCheckLogin
    @ApiOperation(value = "获取当前登录用户信息", httpMethod = "POST", response = R.class, notes = "获取当前登录用户信息")
    public R getCurrentUserInfo() {
        return userService.getCurrentUserInfo();
    }


    @RequestMapping(value = "/getUserMenu", method = RequestMethod.POST)
    @SaCheckLogin
    @ApiOperation(value = "获取用户菜单", httpMethod = "POST", response = R.class, notes = "获取用户菜单")
    public R getUserMenu() {
        return userService.getCurrentUserMenu();
   }

    @GetMapping(value = "/list")
    @SaCheckLogin
    @ApiOperation(value = "查询用户", httpMethod = "POST", response = R.class, notes = "查询用户")
    public R list(Integer pageNo, Integer pageSize, String username, Integer loginType) {
        return userService.listUser(pageNo, pageSize, username, loginType);
    }

    @OperationLogger(value = "获取用户详情")
    @GetMapping("/info")
    @SaCheckPermission("/system/user/info")
    @ApiOperation(value = "获取用户详情", httpMethod = "POST", response = R.class, notes = "获取用户详情")
    public R info(Integer id) {
        return userService.selectById(id);
    }

    @OperationLogger(value = "更新用户信息")
    @SaCheckPermission("/system/user/update")
    @PostMapping("/update")
    @ApiOperation(value = "更新用户信息", httpMethod = "POST", response = R.class, notes = "更新用户信息")
    public R update(@Validated(UpdateGroup.class) @RequestBody SystemUserDTO systemUserDTO) {
        return userService.updateUser(systemUserDTO);
    }

    @OperationLogger(value = "新增用户信息")
    @SaCheckPermission("/system/user/create")
    @PostMapping("/create")
    @ApiOperation(value = "新增用户信息", httpMethod = "POST", response = R.class, notes = "新增用户信息")
    public R create(@Validated(AddGroup.class) @RequestBody SystemUserDTO systemUserDTO) {
        return userService.createUser(systemUserDTO);
    }

    @OperationLogger(value = "删除用户信息")
    @SaCheckPermission("/system/user/remove")
    @DeleteMapping("/remove")
    @ApiOperation(value = "删除用户信息", httpMethod = "DELETE", response = R.class, notes = "删除用户信息")
    public R remove(List<Integer> ids) {
        return userService.removeUser(ids);
    }

    @GetMapping("online")
    @ApiOperation(value = "在线用户列表", httpMethod = "GET", response = R.class, notes = "在线用户列表")
    @SaCheckLogin
    @OperationLogger(value = "在线用户列表")
    public R online(Integer pageNo, Integer pageSize, String keywords) {
        return userService.listOnlineUsers(pageNo ,pageSize ,keywords);
    }

    @GetMapping(value = "/kick")
    @SaCheckPermission("/system/user/kick")
    @OperationLogger(value = "踢人下线")
    @ApiOperation(value = "踢人下线", httpMethod = "GET", response = R.class, notes = "踢人下线")
    public R kick(String token) {
        return userService.kick(token);
    }

}

